// pages/personCenter/addressList/addressList.js
var utils = require("../../../utils/util.js");
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    addressId:null,
    arr: [],
    areaArr:[],
    province:'',
    city:'',
    area:''
  },
  bindChange: function (e) {
    const val = e.detail.value
    this.setData({
      province: this.data.years[val[0]],
      city: this.data.months[val[1]],
      area: this.data.days[val[2]]
    });
  },
  //radio点击事件
  radioChange:function(e){
    var that = this;
    wx.request({
      url: utils.dominame + '/api/v1 / myself / setDefaultAddress',
      data: {
        token: app.globalData.token,
        user_address_id:e.detail.value
      },
      method: 'POST',
      header: { "Content-Type": "application/x-www-form-urlencoded" },
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  //删除
  del:function(e){
    var me=this;
    wx.showModal({
      title: '提示',
      content: '是否删除该地址',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: utils.dominame + '/api/v1/myself/deleteReceivingAddress',
            data: {
              token: app.globalData.token,
              user_address_id: e.currentTarget.id
            },
            method: 'POST',
            header: { "Content-Type": "application/x-www-form-urlencoded" },
            success: function (res) {
              console.log(res);
              if (res.data.code == 0) {
                const index = me.data.arr.findIndex(x => x.id == e.currentTarget.id);
                const arrBox = me.data.arr;
                arrBox.splice(index, 1);
                me.setData({ arr: arrBox});
                wx.showToast({
                  title: res.data.msg,
                  icon: 'success',
                  duration: 2000
                })
              } else {
                wx.showToast({
                   title: res.data.msg,
                  icon: 'none',
                  duration: 2000
                })
              }
            },
            fail: function (res) {
              wx.showToast({
                title: '网络异常',
                icon: 'cancel',
                duration: 2000
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //组织冒泡
  stopProgration:function(){

  },
  //选中地址返回
  goSubmit:function(e){
    if(!this.data.canTap){
      let pages = getCurrentPages();
      let prevPage = pages[pages.length - 2];  //上一个页面
      //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
      prevPage.setData({
        address_id:e.currentTarget.id
      });
      wx.navigateBack({
        delta:1
      });
    }
  },
  //编辑
  edit:function(e){
    wx.navigateTo({
      url: '../editAddress/editAddress?info=' + encodeURI(JSON.stringify(e.currentTarget.dataset.info)),
    })
  },
  //新增
  creatAddress:function(){
    wx.navigateTo({
      url: '../editAddress/editAddress',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ canTap: options.canTap})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      url: utils.dominame + '/api/v1/myself/myReceivingAddressList',
      data: {
        token: app.globalData.token
      },
      method: 'GET',
      header: { "Content-Type": "application/x-www-form-urlencoded" },
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          that.setData({
            arr: res.data.data
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})